home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 004 / gstobject / joystick / Example / c / main
Text File  |  1994-09-25  |  2KB  |  102 lines

  1. /* Test Joystick object.
  2. */
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "kernel.h"
  8.  
  9. /* change the line below to include your copy of the Adhesive
  10.    header file supplied with Adhesive
  11. */
  12. #include "C:h.Adhesive"
  13.  
  14.  
  15. #include "Objects:joystick.149.100.Joystick"
  16. #include "Objects:joystick.149.100.Keys"
  17.  
  18. /* external request table */
  19. extern Adhesive_Request cahg_need_ObjectsNeeded;
  20.  
  21. /* our user handle */
  22. Adhesive_User userHandle=NULL;
  23.  
  24. static void checkErr(_kernel_oserror *err)
  25. { /* does nothing if passed a zero pointer
  26.      else displays error and does not return
  27.   */
  28.   if (err) {
  29.     fprintf(stderr,"Error: %x %s\n",err->errnum,err->errmess);
  30.     exit(EXIT_FAILURE);
  31.    }
  32. }
  33.  
  34.  
  35. static void deRegister(void)
  36. {
  37.   checkErr(adhesive_Deregister(&userHandle));
  38. }
  39.  
  40.  
  41.  
  42. int main()
  43. {
  44.   Adhesive_UserInfo info;
  45.   Joystick j;
  46.   void *workspace;
  47.   int x,y,buttons;
  48.  
  49.   info.flags=0;
  50.   info.name="Test Joystick object";
  51.  
  52.   /* setup atexit handler to ensure we always deregister */
  53.   atexit(deRegister);
  54.  
  55.   /* register with Adhesive */
  56.   checkErr(adhesive_Register(&userHandle,&info));
  57.  
  58.   /* request objects */
  59.   checkErr(adhesive_Request(&userHandle,&cahg_need_ObjectsNeeded));
  60.  
  61.   /* test the joystick */
  62.  
  63.   /* find out size of workspace required for a Joystick
  64.      and allocate it
  65.   */
  66.   workspace = malloc(joystick_PreNew());
  67.  
  68.   /* create a new Joystick which uses this workspace,
  69.      NB do not use the same workspace more than once!
  70.      We shall use physical joystick 1 if it exists.
  71.   */
  72.   j = joystick_New(1,workspace);
  73.  
  74.   /* I (the author) don't have a joystick so we set up
  75.      the cursor keys and space bar to simulate the joystick.
  76.      Comment out the lines below if you have a real joystick,
  77.      you may also need to change the joystick number above.
  78.   */
  79.  
  80.   joystick_AssignKeyX(j,IKEY_LEFT,IKEY_RIGHT);
  81.   joystick_AssignKeyY(j,IKEY_UP,IKEY_DOWN);
  82.   joystick_AssignButtonKey(j,IKEY_SPACE,0);
  83.  
  84.   /* We will now keep printing out the x,y and button
  85.      values until the program is termianted
  86.   */
  87.   while(1) {
  88.     buttons = joystick_Read(j,&x,&y);
  89.     printf("%i \t%i \t%i\n",x,y,buttons);
  90.   }
  91.  
  92.  
  93.   /* We did ever terminate the while loop we would dispose
  94.      of j and associated workspace as follows
  95.   */
  96.   joystick_Dispose(j);
  97.   free(workspace);
  98.  
  99.   return EXIT_SUCCESS;
  100. }
  101.  
  102.